package bll

import (
	. "7qule.com/sd3/CrossServer/src/bll/challenge/model"
)

type globalChallengeStageBll struct {
	// 用户擂台对战的信息
	data map[int]*globalChallengeStageSlice
}

var GlobalChallengeStageBll = &globalChallengeStageBll{}

// --------------------------------初始化--------------------------------------------------

//
// InitData
// @Description: 初始化数据
// @receiver this
//
func (this *globalChallengeStageBll) InitData() (errList []error) {
	tempChallengeMap := GlobalChallengeBll.GetData()

	tempMap := make(map[int]*globalChallengeStageSlice)

	for _, item := range tempChallengeMap {
		if _, ok := tempMap[item.StageId]; !ok {
			tempStageSlice := NewGlobalChallengeStageSlice()
			tempMap[item.StageId] = tempStageSlice
		}
		tempMap[item.StageId].AddChallengeStageLast(item)
	}

	for _, stage := range tempMap {
		stage.Sort()
	}
	this.data = tempMap
	return
}

// --------------------------------生成的基础方法--------------------------------------------------

//
// GetData
// @Description:获取数据
// @receiver this
// @return map[int][]*GlobalChallengeModel:擂台对战的信息
//
func (this *globalChallengeStageBll) GetData() map[int]*globalChallengeStageSlice {
	return this.data
}

// --------------------------------内部方法--------------------------------------------------

//
// getChallengeStageData
// @Description:获取页签信息
// @receiver this
// @param stageId:页签id
// @return challengeSlice:擂台数据
// @return bool:是否存在
//
func (this *globalChallengeStageBll) getChallengeStageData(stageId int) (*globalChallengeStageSlice, bool) {
	result, exist := this.data[stageId]

	return result, exist
}

//
// getChallengeStageTopTen
// @Description:获取已经打过的前十数据
// @receiver this
// @param stageId:页签id
// @param stageSmallestId:最小页签id
// @return map[int]challengeSlice:擂台数据
//
func (this *globalChallengeStageBll) getChallengeStageTopTen(stageId, stageSmallestId int) map[int]challengeSlice {
	result := make(map[int]challengeSlice)
	for i := stageSmallestId; i <= stageId; i++ {
		stageData, exist := this.getChallengeStageData(i)
		if !exist {
			continue
		}
		result[i] = stageData.GetChallengeStageTopTen()
	}

	return result
}

//
// getChallengeStage
// @Description:获取当前页签的擂台数据
// @receiver this
// @param stageId:页签id
// @param rank:排名
// @return challengeSlice:擂台数据
//
func (this *globalChallengeStageBll) getChallengeStage(stageId, rank int) challengeSlice {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return nil
	}
	playerStageSlice := stageData.GetChallengeStage(rank)

	return playerStageSlice
}

// --------------------------------外部方法--------------------------------------------------

//
// GetPlayerChallengeStage
// @Description:获取玩家擂台数据
// @receiver this
// @param stageId:页签id
// @param rank:排名
// @return map[int]challengeSlice:擂台数据
//
func (this *globalChallengeStageBll) GetPlayerChallengeStage(stageId, rank int) map[int]challengeSlice {
	stageSmallestId := BaseChallengeStageModelBll.GetSmallestStage()
	result := make(map[int]challengeSlice)

	if stageId == stageSmallestId {
		playerStageSlice := this.getChallengeStage(stageId, rank)
		result[stageSmallestId] = playerStageSlice

		return result
	}

	result = this.getChallengeStageTopTen(stageId-stageSmallestId, stageSmallestId)
	result[stageId] = this.getChallengeStage(stageId, rank)

	return result
}

//
// GetStageLen
// @Description:获取页签的长度
// @receiver this
// @param stageId:页签id
// @return int:长度
//
func (this *globalChallengeStageBll) GetStageLen(stageId int) int {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return 0
	}
	return stageData.GetChallengeStageLen()
}

//
// AddChallengeStageLast
// @Description:添加用户到页签的末尾
// @receiver this
// @param stageId:页签id
// @param playerChallengeData:用户擂台数据
//
func (this *globalChallengeStageBll) AddChallengeStageLast(stageId int, playerChallengeData *GlobalChallengeModel) {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return
	}

	stageData.AddChallengeStageLast(playerChallengeData)
}

//
// InitAndAddChallengeStageLast
// @Description:初始化并添加到页签末尾
// @receiver this
// @param playerId:用户id
//
func (this *globalChallengeStageBll) InitAndAddChallengeStageLast(playerId string) *GlobalChallengeModel {
	smallestStageId := BaseChallengeStageModelBll.GetSmallestStage()

	stageData, exist := this.getChallengeStageData(smallestStageId)
	if !exist {
		return nil
	}

	playerChallenge := stageData.InitAndAddChallengeStageLast(playerId, smallestStageId)

	return playerChallenge
}

//
// GetChallengePlayerByRank
// @Description:根据排名找玩家
// @receiver this
// @param stageId:页签id
// @param rank:排名
// @return *GlobalChallengeModel:玩家擂台数据
//
func (this *globalChallengeStageBll) GetChallengePlayerByRank(stageId, rank int) *GlobalChallengeModel {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return nil
	}

	result := stageData.GetChallengePlayerByRank(rank)
	return result
}

//
// ExchangeChallengePlayer
// @Description:交换排名
// @receiver this
// @param stageId:页签id
// @param playerRank:挑战者排名
// @param challengedRank:被挑战者排名
//
func (this *globalChallengeStageBll) ExchangeChallengePlayer(stageId, playerRank, challengedRank int) {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return
	}

	stageData.ExchangeChallengePlayer(playerRank, challengedRank)
}

//
// UpdateChallengePlayer
// @Description:修改擂台数据
// @receiver this
// @param stageId:页签id
// @param challengedRank:排名
// @param playerChallenge:用户数据
//
func (this *globalChallengeStageBll) UpdateChallengePlayer(stageId, challengedRank int, playerChallenge *GlobalChallengeModel) {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return
	}

	stageData.UpdatePlayer(challengedRank, playerChallenge)
}
